package com.ry.es.generator;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Map;

public class MappingGenerator {
    private static String[] ignoreFields = { "serialVersionUID", "Fields", "documentId" };//忽略的属性

    public static void generate(Class<?> clazz) {

        Field[] fields = clazz.getDeclaredFields();

        Map<String, Map<String, String>> properties = Maps.newLinkedHashMap();

        Field[] pfields = clazz.getSuperclass().getDeclaredFields();
        for (Field field : pfields) {
            if (ArrayUtils.contains(ignoreFields, field.getName())) {
                continue;
            }
            Map<String, String> property = Maps.newHashMap();
            if (String.class == field.getType()) {
                property.put("type", field.getType().getSimpleName().toLowerCase());
                property.put("index", "not_analyzed");
            } else if (Timestamp.class == field.getType() || Integer.class == field.getType()) {
                property.put("type", "long");
            } else {
                property.put("type", field.getType().getSimpleName().toLowerCase());
            }
            properties.put(field.getName(), property);
        }
        for (Field field : fields) {
            if (ArrayUtils.contains(ignoreFields, field.getName())) {
                continue;
            }
            Map<String, String> property = Maps.newHashMap();
            if (String.class == field.getType()) {
                property.put("type", field.getType().getSimpleName().toLowerCase());
                property.put("index", "not_analyzed");
            } else if (BigDecimal.class == field.getType()) {
                property.put("type", "double");
            } else if (Integer.class == field.getType() || Integer.TYPE == field.getType()) {
                property.put("type", "long");
            } else if (Timestamp.class == field.getType()) {
                property.put("type", "date");
                property.put("format", "yyyy-MM-dd HH:mm:ss");
            }else if (StringUtils.equalsIgnoreCase(field.getType().getSimpleName(), "Location")) {
                property.put("type", "geo_point");
            } else {
                property.put("type", field.getType().getSimpleName().toLowerCase());
            }
            properties.put(field.getName(), property);
        }

        Map<String, Map> map = Maps.newHashMap();
        map.put("properties", properties);

        String content = JSON.toJSONString(map);
        String path = MappingGenerator.class.getClassLoader().getResource("").getPath();
        try {
            File file = new File(StringUtils.substringBefore(path, "target") + "document/elasticsearch-mapping/" + clazz.getSimpleName() + ".mapping");
//            if(!file.exists()){
//                file.createNewFile();
//            }

            FileUtils.writeStringToFile(file, content, "UTF-8");
        }
        catch (IOException e) {
            System.err.println("-----生成elasticsearch mapping文件出错----"+clazz.getName());
        }

    }
}
